package com.ymatou.iapi.sellerproduct.service;

import java.util.Map;

import org.json.JSONObject;

import com.ymt.core.tool.Logger;
import com.ymttest.business.YmatouAutoTestCall;
import com.ymttest.business.service.AssertService;
import com.ymttest.database.mongo.ProductDetailModelWapper;
import com.ymttest.database.mongo.ProductListingModelWapper;
import com.ymttest.database.mongo.ProductStockInfoWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;

public class SetOnShelfUpdateStockNumCall extends YmatouAutoTestCall{

	private static String opurl ="/api/AppProduct/SetOnShelfUpdateStockNum";
	
	public SetOnShelfUpdateStockNumCall() {
		super(EnvSetup.getSystemUrl("sellerproduct_iapi"),opurl,"POST","JSON");
	}
	@Override
	public void addDefHeader() {
		client.addHeader("Content-Type", "application/json");
	}

	public static void main(String[] args) {
		new SetOnShelfUpdateStockNumCall().callAndGetReturnData();
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	public static void checkSetOnShelfUpdateStockNum(String productId , int updateStock) throws Exception {
		
		try{	
			YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
			
			String catalogsID = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0).getsCatalogId();
			//check sql
			int catalogStock = ymtCatalogsIWapper.selectCatalogsByProductId(productId).get(0).getiNum();
			Logger.verifyEquals( updateStock,catalogStock, "SQL-Ymt_Catalogs iNum");
			
			//check Mongo
			if (YmtMongoDriverFactroy.isOldMongoCheck()){
				Map<String, Object> productStockInfoBean = new ProductStockInfoWapper().getByCatalogId(catalogsID);
				AssertService.assertResultEqual(MapUtil.hashMap("ProductId", productId, "CatalogId", catalogsID, "StockNum", updateStock),productStockInfoBean);
			}
			
			
			if (YmtMongoDriverFactroy.isNewMongoCheck()){
				//check ProductDetailModel
				JSONObject productDetailModelBean = new JSONObject(new ProductDetailModelWapper().getProductId(productId).toString());
				int actStock = productDetailModelBean.getJSONArray("CatalogList").getJSONObject(0).getJSONArray("StockList").getJSONObject(0).getInt("Stock");
				Logger.verifyEquals(updateStock, actStock, "ProductDetailModel-Stock");
				
				int CatalogStock = Integer.parseInt(new ProductListingModelWapper().getProductId(productId).get("CatalogStock").toString());
				Logger.verifyEquals(CatalogStock, actStock, "ProductListModel-CatalogStock");
			
			}
			
		}catch(Exception e){
			
		}

	}
	
	
}
